<?php
// 后台管理
class AdminAction extends Action {
	protected $nav = array(
		array('group'=>'index','title'=>'站点设置','url'=>'Admin/config'),
		array('group'=>'index','title'=>'关于我们','url'=>'Admin/config?type=about'),
		array('group'=>'index','title'=>'友情链接','url'=>'Admin/links'),
		array('group'=>'index','title'=>'修改密码','url'=>'Admin/resetPassword'),
		//array('group'=>'index','title'=>'背景音乐','url'=>'Admin/music'),
		array('group'=>'cases','title'=>'作品分组','url'=>'Admin/cases'),
		array('group'=>'cases','title'=>'上传作品','url'=>'Admin/cases_create'),
		array('group'=>'cases','title'=>'新增作品集','url'=>'Admin/cases_group_create'),
		array('group'=>'suite','title'=>'套系管理','url'=>'Admin/suite'),
		array('group'=>'suite','title'=>'新增套系','url'=>'Admin/suite_create'),
		array('group'=>'activity','title'=>'活动管理','url'=>'Admin/activity'),
		array('group'=>'activity','title'=>'新增活动','url'=>'Admin/activity_create'),
		array('group'=>'content','title'=>'关于我们','url'=>'Admin/content?type=about')
	);
	public function _initialize(){
		if(!session('uid')) $this->redirect('Index/login');
	} 
    public function index(){
		$this->config();
	}
	public function config(){
		$Config = M('Config');
		
		if(IS_POST){
			$post = $this->_post();
			$type = $post['type'];
			if($type){
				switch($type){
					
				}
				$data = array(
					'name' => $type,
					'value' => $post['content']
				);
				$v = $Config->where("name = '$type'")->find();
				if($v){
					$r = $Config->where("name = '$type'")->save($data);
				}else{
					$r = $Config->add($data);
				}


			}else{
				$data = array(
					'site_title'=>trim($post['site_title']),
					'site_description'=>trim($post['site_description']),
					'site_statistic'=>trim($post['site_statistic']),
					'site_beian'=>trim($post['site_beian']),
					'address'=>trim($post['address']),
					'webstie'=>trim($post['webstie']),
					'mobile'=>trim($post['mobile']),
					'qq'=>trim($post['qq']),
					'telphone'=>trim($post['telphone']),
					'email'=>trim($post['email']),
					'music'=>trim($post['music'])
				);
				foreach($data as $k=>$v){
					if($Config->where("name ='$k' AND value='$v'")->find()){
						$r = true;
					}else if(!$Config->where("name = '$k'")->find()){
						$r = $Config->add(array('name'=>$k,'value'=>$v));
					}else{
						$r = $Config->where("name ='$k'")->save(array('value'=>$v));
					}
				}
				
			}
			if($r){
				$this->success("保存成功");
			}else{
				$this->success("保存失败");
			}
		}else{	
			$type = trim($this->_param('type'));
			$list = array();
			switch($type){
				case 'about':
					$list['title'] = "关于我们";
				    $list['disabled'] = "disabled";
					$data = $Config->where("name = '$type'")->find();
					$list['content'] = $data["value"];
					$list['type'] = 'about';
					break;
				default:
					$data = $Config->select();
			}
			foreach($data as $k=>$v){
				$list[$v['name']] = $v['value'];
			}
			$this->assign('list',$list);
			$this->assign('nav',$this->nav('index'));
			switch($type){
				case 'about':
					
					$this->display('content');
					break;
				default:
					$this->display('config');
			}
		}
		
	}
	//内容管理
	public function content(){
		$Config = M('Config');
		if(IS_POST){
			$post = $this->_post();
			$data = array(
				'about'=>trim($post['content']),
				'a'=>trim($post['site_description']),
			);
			foreach($data as $k=>$v){
				if($Config->where("name ='$k' AND value='$v'")->find()){
					$r = true;
				}else if(!$Config->where("name = '$k'")->find()){
					$r = $Config->add(array('name'=>$k,'value'=>$v));
				}else{
					$r = $Config->where("name ='$k'")->save(array('value'=>$v));
				}
			}
			if($r){
				$this->success("保存成功");
			}else{
				$this->success("保存失败");
			}
		}else{	
			$data = $Config->select();
			$list = array();
			foreach($data as $k=>$v){
				$list[$v['name']] = $v['value'];
			}
			$this->assign('list',$list);
			$this->assign('nav',$this->nav('content'));
			$this->display('content');
		}
		
	}
	public function cases(){
		
		$param = $this->_param();
		$where = array();
		if($param['id']){
			$Img = M('Img');
			$where['img_group_id'] = $param['id'];
			$list = $Img->where($where)->select();
			foreach($list as $k => $v){
				$list[$k]['img_url'] = $v['img_url'] ? 'http://'.$_SERVER ['HTTP_HOST'].__ROOT__.'/'.$v['img_url']:'http://'.$_SERVER ['HTTP_HOST'].__ROOT__.'/Public/img/noimg.jpg';
				$list[$k]['title'] =  $v['img_title'];
			}
			$this->assign('list',$list);
			$this->assign('nav',$this->nav('cases'));
			//echo $Group->getLastSql();
			//dump($list);
			$this->display('cases_details');
			
		}else{
			$Group = M('Group');
			$list = $Group->alias('A')->join("baixue_img B ON A.id = B.img_group_id")->field('A.*,B.img_url')->group('A.id')->where()->select();
			foreach($list as $k => $v){
				$list[$k]['img_url'] = $v['img_url'] ? 'http://'.$_SERVER ['HTTP_HOST'].__ROOT__.'/'.$v['img_url']:'http://'.$_SERVER ['HTTP_HOST'].__ROOT__.'/Public/img/noimg.jpg';
			}
			
			$this->assign('list',$list);
			$this->assign('nav',$this->nav('cases'));
			$this->display();
		}
		
		
	}
	public function cases_create(){
		if(IS_POST){
			$post = $this->_post();
			$Topics = M('Group');
			$Img = M('Img');

			if(!empty($_FILES)){
				$saveFile = $this->upload();
				if($saveFile){
					$imgURL = $saveFile[0]['savepath'].'M'.$saveFile[0]['savename'];
				}else{
					$imgURL = false;
				}	
			}
			$casesGroupId = trim($post['cases_group_id']);
			if($imgURL){
				$data = array(
					'img_group_id' => $casesGroupId,
					'img_title' => trim($post['img_title']),
					'img_url' => $imgURL,
					'create_time' => datetime(),
					'update_time' => datetime()
				);
				$v = $Img->add($data);	
				if($v){
					$this->success('上传成功');
				}else{
					$this->error('上传失败');
				}
			}else{
				$this->assign('error','请输入内容或者图片');
				$this->display();
			}
		}else{
			$Group = M('Group');
			$cases_group =  $Group->where('status = 1')->select();
			$this->assign('cases_group',$cases_group);
			
			$this->assign('nav',$this->nav('cases'));
			$this->display();
		}
	}
	//删除作品集
	public function cases_delete(){
		if(IS_POST){
			$Group = M('Group');
			$Img = M('Img');
			$post = $this->_post();
			if($post['id']){
				$v = $Group->where("id = {$post['id']}")->delete();
				if($v){
					$this->ajaxReturn(1,"删除成功",1);
				}else{
					$this->ajaxReturn(-1,"删除失败",0);
				}
			}else if($post['itemid']){
				$v = $Img->where("id = {$post['itemid']}")->delete();
				if($v){
					$this->ajaxReturn(1,"删除成功",1);
				}else{
					$this->ajaxReturn(-2,"删除失败",0);
				}
			}else{
				$this->ajaxReturn(-3,"删除失败",0);
			}
		}else{
			$this->ajaxReturn(-4,"删除失败",0);
		}
	}
	public function cases_group_create(){
		if(IS_POST){
			$Group = M('Group');
			$post = $this->_post();
			if($post['cases_title']){
				if($Group->where("title = '{$post['cases_title']}'")->find()){
					$this->ajaxReturn(0,"已存在",0);
				}else{
					$data = array(
						'title' => $post['cases_title'],
						'description' => '',
						'create_time' => datetime(),
						'update_time' => datetime(),
						'status' => 1
					);
					$v = $Group->add($data);
					if($v){
						$this->ajaxReturn(1,"添加成功",1);
					}else{
						$this->ajaxReturn(0,"添加失败",0);
					}
				}
			}else{
				$this->ajaxReturn(0,"标题为空",0);
			}
		}else{
			$this->assign('nav',$this->nav('cases'));
			$this->display();
		}
	}
	public function resetPassword(){
		if(IS_POST){
			$Account = M('Account');
			$post = $this->_post();
			if($post['password']){
				$ac = $Account->where("account ='admin'")->find();
				if($ac){
					$data = array(
						'password'=>MD5($post['password']),
						'update_time' => datetime(),
					);
					$v = $Account->where("uid = {$ac['uid']}")->save($data);
					if($v){
						$this->success("修改成功");
					}else{
						$this->error("修改成功");
					}
				}
			}else{
				$this->error("密码不能为空");
			}
		}else{
			$this->assign('nav',$this->nav('index'));
			$this->display();
		}
	}
	public function suite(){
		$Suite = M('Suites');
		$get = $this->_get();
	
		$list = $Suite->select();
	
		$this->assign('list',$list);
		$this->assign('nav',$this->nav('suite'));
		$this->display();
	}
	public function suite_create(){
		$Suite = M('Suites');
		if(IS_POST){
			$post = $this->_post();
			$data = array(
				'content'=>trim($post['content']),
				'title'=>trim($post['title']),
				'create_time' => datetime()
			);
			if($post['id']){
				$r = $Suite->where("id = {$post['id']}")->save($data);
			}else{
				$r = $Suite->add($data);
			}
			if($r){
				$this->success("保存成功");
			}else{//.$Suite->getLastSql()
				$this->success("保存失败");
			}
		}else{	
			$get = $this->_get();
			if($get['id']){
				$data = $Suite->where("id = {$get['id']}")->find();
			}
			$this->assign('list',$data);
			$this->assign('nav',$this->nav('suite'));
			$this->display();
		}
		
	}
	public function suite_delete(){
		if(IS_POST){
			$Suite = M('Suites');
			$post = $this->_post();
			$post['id'] = trim($post['id']);
			if($post['id']){
				$v = $Suite->where("id = {$post['id']}")->delete();
				if($v) $this->ajaxReturn(1,"删除套系成功",1);
				else $this->ajaxReturn(0,"删除套系失败",0);
			}else{
			    $this->ajaxReturn(0,"ID不存在",-1);
			}
		}else{
		    $this->ajaxReturn(0,"必须为POST提交",-2);
		}
	}
    //活动管理
	public function activity(){
		$Activity = M('Activity');
		$list = $Activity->where()->select();
		foreach($list as $k=>$v){
			if($v['status'] == 1) $list[$k]['status'] = 0;
			else if($v['status'] == 0) $list[$k]['status'] = 1;
		}
		$this->assign('nav',$this->nav('activity'));
		$this->assign('list',$list);
		$this->display();
	}
	//新增活动
	public function activity_create(){
		if(IS_POST){
			$Activity = M('Activity');
			$post = $this->_post();
			if(!empty($_FILES)){
				$saveFile = $this->upload();
				if($saveFile){
					$imgURL = $saveFile[0]['savepath'].'M'.$saveFile[0]['savename'];
				}else{
					$imgURL = false;
				}	
			}
			$casesGroupId = trim($post['cases_group_id']);
			if($imgURL){
				$data = array(
					'title' => trim($post['title']),
					'url' =>  trim($post['url']),
					'img' =>  $imgURL,
					'status' => 1,
					'update_time' => datetime()
				);
				$v = $Activity->add($data);	
				if($v){
					$this->success('新建活动成功');
				}else{
					$this->error('新建活动失败');
				}
			}else{
				$this->assign('error','请上传封面图片');
				$this->display();
			}
		}else{
			$this->assign('nav',$this->nav('activity'));
		    $this->display();
		}
	}
	//更改活动状态
	public function activity_change(){
		$Activity = M('Activity');
		$post = $this->_get();
		$post['id'] = trim($post['id']);
		if($post['id']){
			if($post['status']){
				$data = array(
					'status' => $post['status'],
					'update_time'=> datetime()
				);
			}else{
				$data = array(
					'status' => 0,
					'update_time'=> datetime()
				);
			}
			if($post['status'] == -1) $v = $Activity->where("id = {$post['id']}")->delete();
			else $v = $Activity->where("id = {$post['id']}")->save($data);
			
			if($v) $this->ajaxReturn(1,"操作成功",1);
			else $this->ajaxReturn(0,"操作失败",0);
		}else{
			$this->ajaxReturn(0,"ID不存在",-1);
		}
		
	}
	public function links(){
		$Links = M('Links');
		$list = $Links->where()->select();
		$this->assign('nav',$this->nav('index'));
		$this->assign('list',$list);
		$this->display();
	}
	
	public function links_add(){
		if(IS_POST){
			$Links = M('Links');
			$post = $this->_post();
			$post['title'] = trim($post['title']);
			$post['url'] = trim($post['url']);
			if($post['title'] && $post['url'] ){
				$data = array(
					'title' => $post['title'],
					'url'   => $post['url'],
					'status'=> 1
				);
				$v = $Links->add($data);
				if($v) $this->ajaxReturn($v,"添加友情链接成功",1);
				else $this->ajaxReturn(0,"添加友情链接失败",0);
			}else{
			    $this->ajaxReturn(0,"标题或者URL不存在",-1);
			}
		}else{
		    $this->ajaxReturn(0,"必须为POST提交",-2);
		}
	}
	public function links_delete(){
		if(IS_POST){
			$Links = M('Links');
			$post = $this->_post();
			$post['id'] = trim($post['id']);
			if($post['id']){
				$v = $Links->where("id = {$post['id']}")->delete();
				if($v) $this->ajaxReturn(1,"删除友情链接成功",1);
				else $this->ajaxReturn(0,"删除友情链接失败",0);
			}else{
			    $this->ajaxReturn(0,"ID不存在",-1);
			}
		}else{
		    $this->ajaxReturn(0,"必须为POST提交",-2);
		}
	}


	//上传图片
	public function upload(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 31457280 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->thumb = true;
		$upload->thumbPrefix ='S,M';//缩略图前缀
		$upload->thumbMaxWidth = '200,900';
		$upload->thumbMaxHeight = '300,1200';
		//$path = date('Ymd');
		$upload->savePath =  "Uploads/";// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
	//生成左边导航
	protected function nav($action){
		$nav = $this->nav;
		$getNav = array();
		foreach($nav as $k => $v){
			if($v['group'] == $action){
				$v['url'] = U($v['url']);
				$getNav[] = $v;
			}
		}
		return $getNav;
	}
}